(function($, doc) {

	$.plusReady(function() {
		var user = app.getState();
		var uid = user.user_id;
		var self = plus.webview.currentWebview();
		var roomId = self.roomId;
		var tohash = self.hash;
		var coindata;
		var myWallet;
		if(!tohash) {
			$.alert('hash不存在');
			plus.webview.currentWebview().close();
		}
		/*
		var checkhashUrl = 'https://api.etherscan.io/api?module=account&action=txlistinternal&txhash=' + tohash + '&apikey=STVEU8NV8UAEKRI47485NPKY3CBVDIKRKI';
		$.getJSON(checkhashUrl, function(res) {
			if(res.status != 1) {
				$.alert('交易打包中或交易失败！');
				plus.webview.currentWebview().close();
			}
		});*/
		var providers = ethers.providers;
		var provider = providers.getDefaultProvider();

		//var tohash = "0x27baea23e7d77bff455d94f0c81916f938c398252fb62fce2cdb43643134ce4ed";
		if(tohash.indexOf('-') === -1) {
			provider.getTransaction(tohash).then(function(transaction) {
				//console.log(JSON.stringify(transaction));
				if(!transaction) {
					//$.alert('交易发送失败！');
					//plus.webview.currentWebview().close();
					return;
				}
				if(!transaction.blockNumber) {
					$.alert('交易打包中！');
					//plus.webview.currentWebview().close();
					return;
				}

				provider.getTransactionReceipt(tohash).then(function(transactionReceipt) {
					
					if(!transactionReceipt) {
						$.alert('交易接收中或接收失败！');
						//plus.webview.currentWebview().close();
						return;
					}
				});
			});
		}

		function showlist(list) {
			//console.log(JSON.stringify(list));
			if(!list || list.length < 1) {
				return;
			}
			doc.getElementById('showlist').style.display = '';
			doc.getElementById('listarea').innerHTML = template('listtpl', {
				data: list,
				cname: coindata['name']
			});
			$("#listarea").on('tap', 'li', function() {
				//console.log(this.id);
				plus.runtime.openURL('https://etherscan.io/tx/' + this.id);
			});
		}
		$.getJSON(apiurl + 'trade/getTrade?hash=' + tohash + '&uid=' + uid, function(res) {
			console.log(JSON.stringify(res));
			if(res.status < 1) {
				$.alert('参数错误！');
				plus.webview.currentWebview().close();
				return;
			}
			coindata = ethscan.getCoindata(res.token);
			myWallet = ethscan.getWallet(coindata['ctype']);
			/*
			var coinlist = {};
			ethscan.getmyCoin(function(data) {
				coinlist = data;
			});
			var token = res.token;
			//console.log(JSON.stringify(token));
			coindata = coinlist[token];

			if(!coindata) {
				ethscan.getlist(function(data) {
					coindata = data[token];
					coinlist[token] = coindata;
					localStorage.setItem('mycoinlist' + uid, JSON.stringify(coinlist));
					ethscan.refreshlist();
				});
			}
			var ctype = coindata['ctype'];
			
			if(ctype == 'ethcontact') {
				coindata['abi'] = ethscan.getabi(token);
			}
			if(ctype == 'ethcontact' || ctype == 'eth') {
				myWallet = ethscan.getWallet('eth');
			} else {
				myWallet = ethscan.getWallet('btc');
			}*/
			doc.getElementById('money').innerHTML = res.allmoney + coindata['name'];
			doc.getElementById('remark').innerHTML = res.remark || '';
			doc.getElementById('addtime').innerHTML = app.dateStr(res.addtime);
			if(res.uptime > 0) {
				doc.getElementById('uptimearea').style.display = '';
				doc.getElementById('uptime').innerHTML = app.dateStr(res.uptime);
			}
			var btntype = 0;
			if(res.estatus > 0) {
				doc.getElementById('showcoinsucess').style.display = '';
				if(res.nums < 2) {
					doc.getElementById('showscan').style.display = '';
					doc.getElementById('viewscan').addEventListener('tap', function() {
						//https://etherscan.io/tx/
						plus.runtime.openURL('https://etherscan.io/tx/' + res.gethash);
					});
				} else {
					showlist(res.list);
				}
			} else {
				doc.getElementById('showcoinerror').style.display = '';
				showlist(res.list);
				/*
				if(res.nums > 1) {
					showlist(res.list);
					btntype = 3; //群红包
				} else {
					//转账或单个红包
					if(uid == res.tid && user.eth_address == res.toaddr) {
						btntype = 1;
					} else {
						btntype = 2; //群红包
					}
				}
				*/
				if(res.can > 0) {
					doc.getElementById('btnarea').style.display = '';
				}

			}
			/*
			function dopost(gethash) {
				var postdata = {
					id: res.id,
					uid : uid,
					gethash: gethash,
					toaddr: user.eth_address
				};
				$.post(apiurl + 'trade/setTrade', postdata, function(rt) {
					plus.nativeUI.closeWaiting();
					if(rt.status > 0) {
						$.alert('您收取了' + res.amount + '，请等待对方确认！');
						var chatbox = plus.webview.getWebviewById("chatbox");
						mui.fire(chatbox, 'GET_HONGBAO_SUCC', {
							username: user.user_name,
							money: res.amount,
							coinname: coindata['name']
						});
						plus.webview.currentWebview().close();
						plus.webview.show('chatbox');
					} else {
						$.alert('收款失败！');
					}
				});
			}*/
			doc.getElementById('subbtn').addEventListener('tap', function() {
				//转账或单个红包
				if(res.can < 1) {
					return;
				}
				var postdata = {
					id: res.id,
					uid : uid,
					toaddr: myWallet.address
				};
				$.post(apiurl + 'trade/setTrade', postdata, function(rt) {
					//plus.nativeUI.closeWaiting();
					if(rt.status > 0) {
						$.alert('您收取了' + res.amount + '，请等待对方确认！');
						var chatbox = plus.webview.getWebviewById("chatbox");
						mui.fire(chatbox, 'GET_HONGBAO_SUCC', {
							username: user.user_name,
							money: res.amount,
							coinname: coindata['name'],
							tradeId : res.id,
							tid : res.uid
						});
						plus.webview.currentWebview().close();
						plus.webview.show('chatbox');
					} else {
						$.alert('收款失败！');
					}
				});
				
				//plus.nativeUI.showWaiting("支付中...");
				/*
				var amount = ethers.utils.parseEther(res.amount);
				if(token == 'eth') {
					var wallet = new ethers.Wallet(res.privkey);
					wallet.provider = ethers.providers.getDefaultProvider();
					var sendPromise = wallet.send(user.eth_address, amount);

					sendPromise.then(function(transactionHash) {
						console.log(transactionHash);
					});

					var options = {
						gasLimit: 21000
					};
					var promiseSend = wallet.send(user.eth_address, amount, options);

					promiseSend.then(function(transaction) {
						console.log(transaction);
						$.toast('收款成功！');
						dopost(transaction.hash);
					});
					promiseSend.catch(function(error) {
						console.log(JSON.stringify(error));
						$.toast('收款失败！');
					});
				} else {
					var providers = ethers.providers;
					var provider = providers.getDefaultProvider('homestead');
					var wallet = new ethers.Wallet(res.privkey, provider);
					var contract = new ethers.Contract(coindata['address'], coindata['abi'], wallet);

					var options = {
						//gasPrice: 1100000000, // 1.1Gwei
						gasLimit: 250000
					};
					//plus.nativeUI.closeWaiting();
					var callPromise = contract.transfer(user.eth_address, amount, options);
					callPromise.then(function(obj) {
						console.log(JSON.stringify(obj));
						$.toast('收款成功！');
						dopost(obj.hash);
					})

					callPromise.catch(function(error) {
						console.log(JSON.stringify(error));
						$.toast('收款失败！');
					});
				}*/
			});

		});
	});

}(mui, document));